⚙️ What are Control Statements?
Control statements are used to control the flow of a program — they decide which part of the code will run and how many times.
There are 3 main types:
- Conditional Statements (Decision making)
- Looping Statements (Repetition)
- Jump Statements (Breaking or skipping code)
🧠 1. Conditional Statements (Decision Making)
These allow the program to make decisions based on conditions.
🔹 if Statement
Runs code only if the condition is true.
if a > b:
print("a is greater")
🔹 if–else Statement
Runs one block if condition is true, another if false.
if a > b:
print("a is greater")
else:
print("b is greater")
🔹 if–elif–else Statement
Used when you have multiple conditions.
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")
🔁 2. Looping Statements (Repetition)
Used to repeat a block of code multiple times.
🔹 for Loop
Used when you know how many times to repeat.
for i in range(5):
print(i)
👉 range(5) means 0 to 4.
🔹 while Loop
Used when you don’t know how many times to repeat.
i = 1
while i <= 5:
print(i)
i += 1
⏭️ 3. Jump Statements
Used to control the flow inside loops.
| Statement | Use |
|---|---|
break | Exits the loop completely |
continue | Skips the current iteration and goes to the next one |
pass | Does nothing (used as a placeholder) |
Examples:
for i in range(5):
if i == 3:
break # loop stops when i=3
print(i)
for i in range(5):
if i == 2:
continue # skips when i=2
print(i)
💡 4. Nested Statements
You can use one statement inside another.
if a > 0:
if a % 2 == 0:
print("Positive and Even")
